package com.dzl.exchange;

import com.dzl.base.controller.BaseController;
import com.dzl.exchange.pojo.dto.ExchangeSecretDto;
import com.dzl.exchange.pojo.query.ExchangeSecretQuery;
import com.dzl.exchange.pojo.vo.ExchangeSecretVo;
import com.dzl.exchange.service.WelfareExchangeSecretService;
import com.dzl.util.DateUtil;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 兑换券详细信息表(WelfareExchangeSecretDetails)表控制层
 * @author caobucao
 * @date 2021-07-09 17:27:19
 */
@Api(value = "平台端-兑换券信息表管理", tags = "平台端-兑换券信息表管理")
@RestController
@RequestMapping("/welfareExchangeSecret")
public class WelfareExchangeSecretController extends BaseController {

    @Autowired
    private WelfareExchangeSecretService service;

    /**
     * 分页查询兑换券密表
     * @date 2021/7/9 19:07
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.PageUtils<com.dzl.exchangesecretdetails.pojo.vo.ExchangeSecretVo>
     */
    @GetMapping("/queryExchangeSecretPage")
    public PageUtils<ExchangeSecretVo> queryExchangeSecretPage(ExchangeSecretQuery query) {
        PageInfo<ExchangeSecretVo> pageInfo = service.queryExchangeSecretPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 通过券号或券密查询兑换券详细信息
     * @date 2021/7/31 11:53
     * @author caobucao
     * @param number 券号
     * @param secret 券密
     * @return com.dzl.util.JsonResult<com.dzl.exchangesecret.pojo.vo.ExchangeSecretVo>
     */
    @GetMapping("/queryExchangeSecretByNumberOrSecret")
    public JsonResult<ExchangeSecretVo> queryExchangeSecretByNumberOrSecret(String number, String secret) {
        ExchangeSecretVo vo = service.queryExchangeSecretByNumberOrSecret(number, secret);
        return JsonResult.success(vo);
    }

    /**
     * 批量激活、冻结、解冻兑换券
     * @date 2021/7/10 9:40
     * @author caobucao
     * @param dto 传入参数
     * @return com.dzl.util.JsonResult<java.lang.String>
     */
    @PostMapping("updateBatchExchangeSecretByIds")
    public JsonResult<String> updateBatchExchangeSecretByIds(@RequestBody ExchangeSecretDto dto) {
        if(EmptyUtils.isEmpty(dto.getIds())) {
            return JsonResult.paramErrWithMsg("请选择要激活的兑换券！");
        }
        //如果是激活，才有到期时间
        if(dto.getIsActivate() == 1) {
            if(EmptyUtils.isEmpty(dto.getExpireDate())) {
                return JsonResult.paramErrWithMsg("请选择有效期");
            }
            if(EmptyUtils.isEmpty(dto.getCustomersId())) {
                return JsonResult.paramErrWithMsg("请选择客户！");
            }
            if(EmptyUtils.isEmpty(dto.getSalesId())) {
                return JsonResult.paramErrWithMsg("请选择业务员！");
            }
            Date expireTime = DateUtil.parseDateTime(dto.getExpireDate() + " 23:59:59");
            dto.setExpireTime(expireTime);
            dto.setActivateTime(new Date());
        }
        dto.setUpdater(getAdminInfo().getId());
        dto.setUpdateName(getAdminInfo().getUserName());
        dto.setUpdateTime(new Date());
        int count = service.updateBatchExchangeSecretByIds(dto);
        return count > 0 ? JsonResult.success("共操作"+count+"条数据，操作成功！") : JsonResult.paramErrWithMsg("批量操作失败！");
    }

    /**
     * 描述: 分页查询平台服务端兑换券池列表
     * @date 2022/2/15 14:39
     * @author wangchao
     * @Param [query] 兑换券池列表查询参数
     * @return com.dzl.util.PageUtils<com.dzl.exchange.pojo.vo.ExchangeSecretPageVo> 兑换券池列表信息对象
    **/
    @ApiOperation(value = "分页查询平台服务端兑换券池列表", notes = "分页查询平台服务端兑换券池列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页大小", defaultValue = "10", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "isActivate", value = "兑换券激活状态；0：未激活，1：激活，2：已冻结 3：已绑定 4：已使用", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "number", value = "券号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "batchNumber", value = "批次号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "绑定用户手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "enterpriseId", value = "来源企业ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "ruleType", value = "规则类型：1.选商品 2.选套餐", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/queryExchangeSecretListPage")
    public PageUtils<ExchangeSecretVo> queryExchangeSecretListPage(ExchangeSecretQuery query) {
        try {
            query.setOrderByKey("a.id desc");
            return new PageUtils<>(service.queryExchangeSecretPage(query));
        }catch (Exception e){
            e.printStackTrace();
        }
        return new PageUtils<>();
    }
}
